﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Training.Library;
using Training.Library.DomainObject.Transaction;
using Training.Library.Enum;
using Training.Library.Interfaces;

namespace Training.Library.DataAccess.Transaction
{
    public partial class PelatihanDetailRepo
    {
        public object SelectScalar(SQL.Function.Aggregate function, string column,string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                      
                        case SQL.Function.Aggregate.Count:
                            //sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.PelatihanDetail {1}", column,parameters);
                            cmd.CommandText = "SP_PelatihanDetail_GridPage_CountData";
                            break;
                       
                        default:
                            // do nothing 
                            break;
                    }

                    //cmd.CommandText = sbQuery.ToString();
                    //cmd.CommandType = CommandType.Text;

                   
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@column", SqlDbType.VarChar).Value = column;
                    cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = parameters;

                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<PelatihanDetail> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<PelatihanDetail> tt = new List<PelatihanDetail>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    //StringBuilder sbQuery = new StringBuilder();
                    //sbQuery.AppendFormat(" WITH result_set AS ");
                    //sbQuery.AppendFormat(" ( ");
                    //sbQuery.AppendFormat("    SELECT ");
                    //sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    //sbQuery.AppendFormat("      PelatihanDetail.DetailID, PelatihanDetail.PelatihanID,PelatihanDetail.NIK,PelatihanDetail.CreatedDate,PelatihanDetail.CreatedBy,PelatihanDetail.UpdatedDate, PelatihanDetail.UpdatedBy ");
                    //sbQuery.AppendFormat("    FROM ");
                    //sbQuery.AppendFormat("      transactions.PelatihanDetail INNER JOIN master.Karyawan ON PelatihanDetaiL.NIK = Karyawan.NIK  INNER JOIN DBJobsDesc.master.Position ON Karyawan.PositionID = Position.PositionID INNER JOIN DBJobsDesc.master.Department ON Department.DeptID = Position.DepartmentID INNER JOIN master.Cabang ON Karyawan.CabangID = Cabang.CabangID INNER JOIN master.Lokasi ON Karyawan.LokasiID = Lokasi.LokasiID ");
                    //sbQuery.AppendFormat("      {0} ",parameters);
                    //sbQuery.AppendFormat(" ) ");
                    //sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    //cmd.CommandText = sbQuery.ToString();
                    //cmd.CommandType = CommandType.Text;

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "SP_PelatihanDetail_GridPage";
                    cmd.Parameters.Add("@startRow", SqlDbType.Int).Value = startRow;
                    cmd.Parameters.Add("@endRow", SqlDbType.Int).Value = endRow;
                    cmd.Parameters.Add("@sortName", SqlDbType.VarChar).Value = sortName;
                    cmd.Parameters.Add("@sortOrder", SqlDbType.VarChar).Value = sortOrder;
                    cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = parameters;

                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                PelatihanDetail t = new PelatihanDetail();
                                t.DetailID = dr[PelatihanDetail.Fields.DetailID] == DBNull.Value ? 0 : int.Parse(dr[PelatihanDetail.Fields.DetailID].ToString());
                                t.PelatihanID = dr[PelatihanDetail.Fields.PelatihanID] == DBNull.Value ? (int?)null : int.Parse(dr[PelatihanDetail.Fields.PelatihanID].ToString());
                                t.NIK = dr[PelatihanDetail.Fields.NIK] == DBNull.Value ? string.Empty : dr[PelatihanDetail.Fields.NIK].ToString();
                                t.CreatedDate = dr[PelatihanDetail.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PelatihanDetail.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[PelatihanDetail.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[PelatihanDetail.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[PelatihanDetail.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PelatihanDetail.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[PelatihanDetail.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[PelatihanDetail.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public DataTable GetDataTableReport(string param)
        {
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {

                    //StringBuilder sbQuery = new StringBuilder();

                    //sbQuery.AppendLine("SELECT");
                    //sbQuery.AppendLine("ROW_NUMBER() OVER(ORDER BY C.NamaPeserta ASC) AS No");
                    //sbQuery.AppendLine(",C.NIK");
                    //sbQuery.AppendLine(",C.NamaPeserta");
                    //sbQuery.AppendLine(",B.NamaPelatihan");
                    //sbQuery.AppendLine(",convert(varchar,B.TglPelatihan , 105) TglPelatihan");
                    //sbQuery.AppendLine(",B.TempatPelatihan");
                    //sbQuery.AppendLine(",D.NamaPembicara Pembicara");
                    //sbQuery.AppendLine(",E.PositionName Jabatan");
                    //sbQuery.AppendLine("FROM transactions.PelatihanDetail A");
                    //sbQuery.AppendLine("INNER JOIN master.Pelatihan B ON A.PelatihanID = B.PelatihanID");
                    //sbQuery.AppendLine("INNER JOIN master.Peserta C ON A.PesertaID = C.PesertaID");
                    //sbQuery.AppendLine("INNER JOIN master.Pembicara D ON B.PembicaraID = D.PembicaraID");
                    //sbQuery.AppendLine("INNER JOIN DBJobsDesc.master.Position E ON C.PositionID = E.PositionID");
                    //sbQuery.AppendLine(param);
                    //sbQuery.AppendLine("ORDER BY C.NamaPeserta ASC,B.NamaPelatihan ASC ");

                    //cmd.CommandText = sbQuery.ToString();

                    cmd.CommandText = "SP_Pelatihan_Report_Detail";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = param;

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;

                    try
                    {
                        conn.Open();
                        da.Fill(dt);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }

            return dt;

        }
    }
}
